package com.song.day02定界符编码器.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.FileRegion;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.io.File;
import java.io.RandomAccessFile;

public class ServerHandler  extends  ChannelInboundHandlerAdapter{
	
	int  counter=0;
	
	public void channelRead(ChannelHandlerContext ctx, Object obj)
			throws Exception {
		String body  = (String)obj;
		System.out.println("this is "+ ++counter +" server receive  client msg "+obj);
		body+="$_";
		ByteBuf echo  =Unpooled.copiedBuffer(body.getBytes());
		ctx.writeAndFlush(echo);
	}


	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		System.out.println(cause.getCause());
		ctx.close();
	}


}
